package com.cskaoyan.controller;

import com.cskaoyan.bean.Admin;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("admin/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @GetMapping("list")
    public BaseRespVo listAdmin(Integer page,Integer limit,
                                String sort,String order,String username){
        Map<String,Object> map = adminService.listAdmin(page,limit,sort,order,username);
        return BaseRespVo.ok(map);
    }
    @PostMapping("create")
    public BaseRespVo createAdmin(@RequestBody Admin admin){
        if (admin.getPassword().length() < 6){
            return BaseRespVo.fail602();
        }
        Map<String,Object> map = null;
        try {
            map = adminService.createAdmin(admin);
        }catch (Exception e){
            return BaseRespVo.fail601();
        }
        return BaseRespVo.ok(map);
    }
    @PostMapping("update")
    public BaseRespVo updateAdmin(@RequestBody Admin admin){
        if (admin.getPassword().length() < 6){
            return BaseRespVo.fail602();
        }
        Map<String,Object> map = null;
        try {
            map = adminService.updateAdmin(admin);
        }catch (Exception e){
            return BaseRespVo.fail601();
        }
        return BaseRespVo.ok(map);
    }
    @PostMapping("delete")
    public BaseRespVo deleteAdmin(@RequestBody Admin admin){
        Integer delete = adminService.deleteAdmin(admin);
        if (delete != 1){
            return BaseRespVo.fail("系统繁忙");
        }
        return BaseRespVo.ok();
    }
}
